package com.example.fileserver_API.Interfaces.Service.Query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fileserver_API.Dao.UploadpicturesMappers;
import com.example.fileserver_API.Entity.Dto.PageParmar.PageParmar;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.Interfaces.IService.Query.IQueryservice;
import com.example.fileserver_API.utils.APIResultUtils;
import com.example.fileserver_API.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class Queryservice implements IQueryservice {

    @Resource
    UploadpicturesMappers uploadpicturesMappers;

    /**
     * 统计查询获取数据
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetInfo(PageParmar pageParmar) {
        if (pageParmar==null)
            pageParmar = new PageParmar();
        String resouretype = "";// 资源类型[图片]/[资源]/[视频]/[APP]
        String oldfilename = "";// 源文件名
        String newfilename = "";// 新文件名
        String suffix = "";// 文件后缀
        String filetype = "";// 文件分类文件夹
        String filepath = "";// 文件夹位置 备注：在设置的文件的文件夹中会存在UserPic、ArticlePic等等中的某个文件夹内的数据
        Object argsObject = pageParmar.getArgs();
        if (argsObject instanceof Map) {
            Map<String, Object> argsMap = (Map<String, Object>) argsObject;
            // 假设 "typename" 的值是字符串类型
            resouretype = (String) argsMap.getOrDefault("resouretype", "");
            oldfilename = (String) argsMap.getOrDefault("oldfilename", "");
            newfilename = (String) argsMap.getOrDefault("newfilename", "");
            suffix = (String) argsMap.getOrDefault("suffix", "");
            filetype = (String) argsMap.getOrDefault("filetype", "");
            filepath = (String) argsMap.getOrDefault("filepath", "");
        }
        QueryWrapper<uploadpictures> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(resouretype))
            queryWrapper.eq("resouretype",resouretype);
        if (StringUtils.isNotBlank(oldfilename))
            queryWrapper.eq("oldfilename",oldfilename);
        if (StringUtils.isNotBlank(newfilename))
            queryWrapper.eq("newfilename",newfilename);
        if (StringUtils.isNotBlank(suffix))
            queryWrapper.eq("suffix",suffix);
        if (StringUtils.isNotBlank(filetype))
            queryWrapper.eq("filetype",filetype);
        if (StringUtils.isNotBlank(filepath))
            queryWrapper.eq("filepath",filepath);
        IPage<uploadpictures> iPage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<uploadpictures> data = uploadpicturesMappers.selectPage(iPage,queryWrapper);
        return APIResultUtils.success("",data);
    }
}
